package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper mapper;
    @Override
    public IPage<UserBean> findByItem(Integer pageNO, String username, LocalDate startDate,LocalDate endDate) {

        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        if (username!=null&&!"".equals(username)){
            qw.like("u_username",username);
        }
        if (startDate!=null){
            qw.ge("u_birthday",startDate);
        }if (endDate!=null){
            qw.le("u_birthday",endDate);
        }

        return mapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public void add(UserBean user) {
        mapper.insert(user);
    }

    @Override
    public void update(Integer id, Integer salary) {
        UserBean user = mapper.selectById(id);
        user.setSalary(salary);

        mapper.updateById(user);
    }

    @Override
    public void del(Integer id) {
    mapper.deleteById(id);
    }

    @Override
    public UserBean findById(Integer id) {
        return mapper.selectById(id);
    }
}
